/*
 * GoogleWebAlbumPhoto.java
 *
 * Created on May 16, 2007, 12:50:42 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webalbum.google;

import com.google.gdata.data.media.mediarss.MediaContent;
import com.google.gdata.data.media.mediarss.MediaGroup;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.photos.PhotoEntry;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import webalbum.WebAlbumPhoto;

/**
 *
 * @author zheismann
 */
public class GoogleWebAlbumPhoto implements WebAlbumPhoto
{
    private PhotoEntry photoEntry;
    
    public GoogleWebAlbumPhoto(PhotoEntry pe)
    {
        photoEntry = pe;
    }
    public String getName()
    {
        return (photoEntry.getTitle() != null ? photoEntry.getTitle().getPlainText() : "");
    }
    public String getId()
    {
        return photoEntry.getId();
    }
    public String getDescription()
    {
        return (photoEntry.getDescription() != null ? photoEntry.getDescription().getPlainText() : "");
    }
    public URI getURI()
    {
        URI uri = null;
        MediaGroup mg = photoEntry.getExtension(MediaGroup.class);
        if (mg != null)
        {
            List<MediaContent> mediaContents = mg.getContents();
            if (mediaContents != null && mediaContents.size() > 0)
            {
                try
                {
                    uri = new java.net.URI(mediaContents.get(0).getUrl());
                }
                catch (URISyntaxException ex)
                {
                    Logger.getLogger("global").log(Level.SEVERE, null, ex);
                }
            }
        }
        return uri;
    }
    public URI getThumbnailURI()
    {
        URI uri = null;
        MediaGroup mg = photoEntry.getExtension(MediaGroup.class);
        if (mg != null)
        {
            List<MediaThumbnail> mediaThumbnails = mg.getThumbnails();
            if (mediaThumbnails != null)
            {
                int minSize = 0;
                MediaThumbnail smallestThumbnail = null;
                for (MediaThumbnail mt : mediaThumbnails)
                {
                    if (smallestThumbnail == null)
                    {
                        smallestThumbnail = mt;
                        minSize = mt.getHeight() + mt.getWidth();
                    }
                    else if (minSize > (mt.getHeight() + mt.getWidth()))
                    {
                        smallestThumbnail = mt;
                        minSize = mt.getHeight() + mt.getWidth();
                    }
                }
                if (smallestThumbnail != null)
                {
                    try
                    {
                        uri = new java.net.URI(smallestThumbnail.getUrl());
                    }
                    catch (URISyntaxException ex)
                    {
                        Logger.getLogger("global").log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        return uri;
    }

    public Long getWidth() throws Exception
    {
        return photoEntry.getWidth();
    }
    public Long getHeight() throws Exception
    {
        return photoEntry.getHeight();
    }
    public Long getSize() throws Exception
    {
        return photoEntry.getSize();
    }
    public Date getTimestamp() throws Exception
    {
        return photoEntry.getTimestamp();
    }
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("name: " + getName()).append("\n\tid: " + getId())
                .append("\n\tURI: " + getURI())
                .append("\n\tThumbnail: " + getThumbnailURI());
        try 
        {
            sb.append("\n\tWidth: " + getWidth()).append("\n\tHeight: " + getHeight()).append("\n\tSize: " + getSize());
        }
        catch (Exception ex) 
        {
            Logger.getLogger("global").log(Level.SEVERE, null, ex);
        }
        return sb.toString();
    }
}
